<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="80px" style="padding: 0 20px;">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="规则代码" style="margin-bottom: 5px;">
              <el-input v-model="searchData.ruleCode" size="medium" class="select-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="规则名称" style="margin-bottom: 5px;">
              <el-input v-model="searchData.ruleName" size="medium" class="select-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="执行频率" style="margin-bottom: 5px;">
              <el-select v-model="searchData.ruleFrequency" size="medium" class="select-item">
                <el-option
                  v-for="item in frequencyOption"
                  :key="item.index"
                  :label="item.label"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div style="margin-top: 3px;">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="add-form">
      <el-button icon="el-icon-plus" v-show="tableOption.isShowOperate" type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>
    <div ref="ref" :class="tableOption.isShowOperate?'table-box':'table-box2'">
      <TableComponent :columns="tableColumns" :height="height" :data="tableData" @change="onTableChange" :tableOption="tableOption" :page-option="searchData.page" :stripe="stripe" :total="total">
        <template slot-scope="scope">
          <el-button @click="editRow(scope.scope.row)" type="text" size="mini">编辑</el-button>
          <el-button @click="delRow(scope.scope.row)" type="text" size="mini">删除</el-button>
        </template>
      </TableComponent>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisibleAdd" width="60%">
      <div class="dialog-box">
        <el-form ref="form" :model="addForm" label-width="100px" :rules="rules" label-position="left" style="padding: 0 20px;">
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="规则代码" prop="ruleCode" style="margin-bottom: 12px;">
                <el-input v-model="addForm.ruleCode" size="small"></el-input>
              </el-form-item>
              <el-form-item label="规则名称" prop="ruleName" style="margin-bottom: 12px;">
                <el-input v-model="addForm.ruleName" size="small"></el-input>
              </el-form-item>
              <el-form-item label="执行频率" prop="ruleFrequency" style="margin-bottom: 12px;">
                <el-select v-model="addForm.ruleFrequency" size="small" class="select-item">
                  <el-option
                    v-for="item in frequencyOption"
                    :key="item.index"
                    :label="item.label"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="参数一" prop="paramOne" style="margin-bottom: 12px;">
                <el-input v-model="addForm.paramOne" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预警指标" prop="indexCode" style="margin-bottom: 12px;">
                <el-select v-model="addForm.indexCode" size="medium" class="select-item">
                  <el-option
                    v-for="item in indexCodeOption"
                    :key="item.index"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="规则类型" prop="ruleType" style="margin-bottom: 12px;">
                <el-select v-model="searchData.ruleType" size="medium" class="select-item">
                  <el-option
                    v-for="item in ruleTypeOption"
                    :key="item.index"
                    :label="item.label"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
               <el-form-item label="执行时间" prop="ruleRunDate" style="margin-bottom: 12px;">
                <el-date-picker v-model="addForm.ruleRunDate" size="small" class="select-item" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
              <el-form-item label="参数二" prop="paramTwo" style="margin-bottom: 12px;">
                <el-input v-model="addForm.paramTwo" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { queryAlertRules, addAlertRules, querySingleAlertRule, editAlertRules, delAlertRules, indeCodeOption } from '@/api/modules/warning.js'
import { tableColumns, ruleTypeOption, frequencyOption } from './index.js'
export default {
  components: {
    TableComponent
  },
  data() {
    return {
      searchData: {
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      ruleTypeOption,
      frequencyOption,
      tableColumns,
      tableData: [],
      height: null,
      title: '',
      total: 0,
      tableOption: {
        isShowOperate: true,
        operateWidth: '100px'
      },
      stripe: true,
      dialogVisibleAdd: false,
      addForm: {},
      currencyOption: [],
      indexCodeOption: [],
      rules: {
        ruleCode: [{ required: true, message: '请输入规则代码', trigger: 'blur' }]
      },
      saveRuleCode: {
        ruleCode: ''
      }
    }
  },
  mounted() {
    const key = this.$store.state.key.resourceCode
    if (key === '0') {
      this.tableOption.isShowOperate = false
    } else {
      this.tableOption.isShowOperate = true
    }
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref.offsetHeight
    }
    this.initData(this.searchData)
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    initData(params) {
      queryAlertRules(params).then(res => {
        if (res.data.code === '0') {
          this.tableData = res.data.body.list
          this.total = res.data.body.total
        }
      })
    },
    // 点击查询
    handleSearch() {
      this.initData(this.searchData)
    },
    // 点击新增
    handleAdd() {
      this.dialogVisibleAdd = true
      this.title = '新增'
      indeCodeOption().then(res => {
        this.indexCodeOption = res.data.body
      })
    },
    // 点击确定新增/修改
    handleSubmit(formName) {
      if (this.title === '新增') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addAlertRules(this.addForm).then(res => {
              if (res.data.code === '0') {
                this.resetForm('form')
                this.dialogVisibleAdd = false
                this.$message.success('新增成功')
                this.initData(this.searchData)
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      } else if (this.title === '修改') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = Object.assign(this.addForm, this.saveRuleCode)
            editAlertRules(params).then(res => {
              if (res.data.code === '0') {
                this.resetForm('form')
                this.dialogVisibleAdd = false
                this.initData(this.searchData)
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      }
      
    },
    editRow(row) {
      console.log(row);
      this.saveRuleCode.ruleCode = row.ruleCode
      this.dialogVisibleAdd = true
      this.title = '修改'
      const params = {
        ruleCode: row.ruleCode
      }
      querySingleAlertRule(params).then(res => {
        if (res.data.code === '0') {
          this.addForm = res.data.body
        }
      })
    },
    delRow(row) {
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          ruleCode: row.ruleCode
        }
        delAlertRules(params).then(res => {
          if (res.data.code === '0') {
            this.$message.success('删除成功')
            this.initData(this.searchData)
          } else {
            this.$message.error(res.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    onTableChange(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    
    handleClear() {
      this.searchData.ruleCode = ''
      this.searchData.ruleName = ''
      this.searchData.ruleType = ''
      this.searchData.ruleFrequency = ''
    },
    handleCancle() {
      this.resetForm('form')
      this.dialogVisibleAdd = false
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
    
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .add-form {
      padding: 10px 20px;
      background-color: #ffffff;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 260px);
      .table {
        height: 100%;
      }
    }
    .table-box2 {
      width: 100%;
      height: calc(100vh - 228px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
